package com.yutel.amote.service;

import android.app.Instrumentation;
import android.content.Intent;
import android.inputmethodservice.InputMethodService;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;

import com.yutel.amote.packet.ServerHelper;

public class ServerService extends InputMethodService {
	final String TAG = "sokect";
	private ServerHelper sh;
	Instrumentation inst;
	@Override
	public void onCreate() {
		super.onCreate();
		AmoteConstant con = AmoteConstant.getInstance(null);
		sh = new ServerHelper(Integer.parseInt(con.getKey("serverPort")),this);
		inst=new Instrumentation();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		sh.stop();
		sh.finalize();
		Log.d(TAG, "sokect service stop");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		if(!sh.isAlive()){
			sh.start();
		}
		Log.d(TAG, "sokect service start");
	}
	public void sendMsg(Object msg){
        Intent intent = new Intent("com.webabcd.service.msg");
        intent.putExtra("msg", (String)msg);
        this.sendBroadcast(intent);
        sendEvent((String)msg);
	}
	public void sendEvent(String msg) {
		String[] es=msg.split(";");
		String[] e1=es[0].split(",");
		String[] e2=es[1].split(",");
		int e1x=Integer.parseInt(e1[0]);
		int e1y=Integer.parseInt(e1[1]);
		int e2x=Integer.parseInt(e2[0]);
		int e2y=Integer.parseInt(e2[1]);
		inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN, e1x, e1y, 0));
		inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),MotionEvent.ACTION_UP, e2x, e2y, 0));
	}
}
